package com.tianyi.wz.walkinggame.biz;

import android.app.Activity;
import android.util.Log;
import android.view.ViewGroup;

import com.qq.e.ads.nativ.ADSize;
import com.qq.e.ads.nativ.NativeExpressAD;
import com.qq.e.ads.nativ.NativeExpressADView;
import com.qq.e.comm.util.AdError;
import com.tianyi.wz.walkinggame.Constant;
import com.tianyi.wz.walkinggame.common.ToastCommon;

import java.util.List;

public class TbsBannerBiz implements NativeExpressAD.NativeExpressADListener,IBannerBiz {

    private NativeExpressAD mNativeExpressAD;

    private Activity mActivity;
    private ViewGroup mContainer;
    private String mCodeId;
    private String mUserId;
    private Callback callback;

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public static TbsBannerBiz getInstance(Activity activity, ViewGroup container, String userId, String codeId){
        TbsBannerBiz biz = new TbsBannerBiz(activity,container,userId,codeId);
        return biz;
    }

    private TbsBannerBiz(Activity activity, ViewGroup container,String userId,String codeId){
        mActivity = activity;
        mCodeId = codeId;
        mUserId = userId;
        mContainer = container;
        init();
    }

    private void init(){
        mNativeExpressAD = new NativeExpressAD(
                mActivity,
                new ADSize(280,ADSize.AUTO_HEIGHT),
                Constant.SETTING.TBS_APPID,
                mCodeId,
                this);
    }

    public void showAd(){
        mNativeExpressAD.loadAD(1);
    }

    @Override
    public void onNoAD(AdError adError) {
        mContainer.removeAllViews();
        //ToastCommon.showToast(mActivity,adError.getErrorMsg() + " code: " + adError.getErrorCode());
        Log.d(this.getClass().getName(),adError.getErrorMsg());
    }

    @Override
    public void onADLoaded(List<NativeExpressADView> list) {
        NativeExpressADView nativeExpressADView = list.get(0);
        mContainer.removeAllViews();
        mContainer.addView(nativeExpressADView);
        nativeExpressADView.render();
        new LogBiz(mActivity).saveAd(mUserId,mCodeId,2);
    }

    @Override
    public void onRenderFail(NativeExpressADView nativeExpressADView) {
        mContainer.removeAllViews();
        mContainer.addView(nativeExpressADView);
        Log.d(this.getClass().getName(),"render fail...");
    }

    @Override
    public void onRenderSuccess(NativeExpressADView nativeExpressADView) {
        if(callback!=null){
            callback.call();
        }
    }

    @Override
    public void onADExposure(NativeExpressADView nativeExpressADView) {

    }

    @Override
    public void onADClicked(NativeExpressADView nativeExpressADView) {

    }

    @Override
    public void onADClosed(NativeExpressADView nativeExpressADView) {

    }

    @Override
    public void onADLeftApplication(NativeExpressADView nativeExpressADView) {

    }

    @Override
    public void onADOpenOverlay(NativeExpressADView nativeExpressADView) {

    }

    @Override
    public void onADCloseOverlay(NativeExpressADView nativeExpressADView) {

    }

}
